package com.example.mvvmtemplate.utli.permission;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentManager;

public class JavaPermissionX {

    private static class PermissionXHolder {
        private static final JavaPermissionX INSTANCE = new JavaPermissionX();
    }

    private JavaPermissionX() {
    }

    public static JavaPermissionX getInstance() {
        return PermissionXHolder.INSTANCE;
    }

    private static final String TAG = "InvisibleFragment";

    public void request(FragmentActivity activity, InvisibleFragment.PermissionCallback callback, String... permissions) {
        FragmentManager fragmentManager = activity.getSupportFragmentManager();
        Fragment existedFragment = fragmentManager.findFragmentByTag(TAG);
        InvisibleFragment fragment;
        if (existedFragment != null) {
            fragment = (InvisibleFragment) existedFragment;
        } else {
            InvisibleFragment invisibleFragment = new InvisibleFragment();
            fragmentManager.beginTransaction().add(invisibleFragment, TAG).commitNow();
            fragment = invisibleFragment;
        }
        fragment.requestNow(callback, permissions);
    }
}
